package com.cloudcc.mobile.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;

/**
 * @author wangzhiqiang
 * @Description: cloudcc标题栏
 * @date 2015-12-14 下午9:24:32
 */
public class CloudCCTitleBar extends RelativeLayout implements OnClickListener {
    private ImageView mLeftImage;
    private TextView mLeftText;
    private TextView mTitle;
    private ProgressBar mProgress;
    private ImageView mRightImage;
    private TextView mRightText;
    private RelativeLayout mLeftLayout;
    private FrameLayout mRightLayout;
    private OnTitleBarClickListener mOnTitleBarClickListener;

    public CloudCCTitleBar(Context context) {
        this(context, null);
    }

    public CloudCCTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CloudCCTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        stepView();
        parseStyle(context, attrs);
    }

    private void stepView() {
        LayoutInflater.from(getContext())
                .inflate(R.layout.header_cloudcc, this);
        mLeftImage = (ImageView) findViewById(R.id.left_image);
        mLeftText = (TextView) findViewById(R.id.left_text);
        mProgress = (ProgressBar) findViewById(R.id.progressbar);
        mTitle = (TextView) findViewById(R.id.header_title);
        mRightLayout = (FrameLayout) findViewById(R.id.right_layout);
        mRightImage = (ImageView) findViewById(R.id.right_image);
        mRightText = (TextView) findViewById(R.id.right_text);
        mLeftLayout = (RelativeLayout) findViewById(R.id.left_layout);
        mLeftImage.setOnClickListener(this);
        mLeftText.setOnClickListener(this);
        mRightImage.setOnClickListener(this);
        mRightText.setOnClickListener(this);
        mLeftLayout.setOnClickListener(this);
        mRightLayout.setOnClickListener(this);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs,
                    R.styleable.CloudCCBar);
            String title = ta.getString(R.styleable.CloudCCBar_titleBarTitle);
            setTitle(title);

            Drawable leftDrawable = ta
                    .getDrawable(R.styleable.CloudCCBar_titleBarLeftImage);
            if (null != leftDrawable) {
                setLeftImage(leftDrawable);
            }
            String rightText = ta
                    .getString(R.styleable.CloudCCBar_titleBarRightText);
            setRightText(rightText);
            Drawable rightDrawable = ta
                    .getDrawable(R.styleable.CloudCCBar_titleBarRightImage);
            if (null != rightDrawable) {
                setRightImage(rightDrawable);
            }
            ta.recycle();
        }
    }

    /**
     * 设置titile
     *
     * @param title
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * 设置右边的
     *
     * @param text
     */
    public void setRightText(String text) {
        mRightImage.setVisibility(View.GONE);
        mRightText.setVisibility(View.VISIBLE);
        mRightText.setText(text);
        // mRightText.setTextSize(500);
    }

    /**
     * 设置左边图片
     *
     * @param resId
     */
    public void setLeftImageResource(int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        setLeftImage(drawable);
    }

    /**
     * 设置 左边图片
     *
     * @param iamge
     */
    public void setLeftImage(Drawable iamge) {
        mLeftImage.setImageDrawable(iamge);
    }

    /**
     * 设置右边图片
     *
     * @param resId
     */
    public void setRightImageResource(int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        setRightImage(drawable);
    }
    /**
     * 设置左边显示文字，并设置文字
     *
     * */
    public void setmLeftText(String lefttexts){
        mLeftImage.setVisibility(GONE);
        mLeftText.setVisibility(VISIBLE);
        if(lefttexts != null && lefttexts.length() != 0){
            mLeftText.setText(lefttexts);
        }
    }

    /**
     * 设置右边的图片
     *
     * @param iamge
     */
    public void setRightImage(Drawable iamge) {
        mRightImage.setVisibility(View.VISIBLE);
        mRightText.setVisibility(View.GONE);
        mRightImage.setImageDrawable(iamge);
    }

    public void setImage() {
        mRightLayout.setVisibility(View.GONE);
        mRightImage.setVisibility(View.GONE);
    }

    /**
     * 开始
     */
    public void beginProgress() {
        mProgress.setVisibility(View.VISIBLE);
    }

    /**
     * 结束
     */
    public void completeProgress() {
        mProgress.setVisibility(View.GONE);
    }

    public interface OnTitleBarClickListener {
        void onClickLeft(View v);

        void onClickRight(View v);
    }

    /**
     * 设置监听
     *
     * @return
     */
    public OnTitleBarClickListener getOnTitleBarClickListener() {
        return mOnTitleBarClickListener;
    }

    public void setOnTitleBarClickListener(
            OnTitleBarClickListener bnTitleBarClickListener) {
        this.mOnTitleBarClickListener = bnTitleBarClickListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_image:// 左侧
            case R.id.left_text:
            case R.id.left_layout:// 左侧
                if (mOnTitleBarClickListener != null) {
                    mOnTitleBarClickListener.onClickLeft(v);
                }
                break;
            case R.id.right_layout:
            case R.id.right_image:
            case R.id.right_text:
                if (mOnTitleBarClickListener != null) {
                    mOnTitleBarClickListener.onClickRight(v);
                }
                break;

            default:
                break;
        }
    }

    /**
     * 获得右边的布局
     *
     * @return
     */
    public View getRightLayout() {
        View findViewById = findViewById(R.id.right_layout);
        return findViewById;
    }

}
